import Api, { Response } from "@/src/lib/api/api"
import Taro from '@tarojs/taro'
import InitModel from "@/src/lib/models/init_model"

export interface ConfigState {
    
    // 取消订单原因记录
    orderCloseReasons: string[],

    // 申请退款原因
    orderBackmoneyReasons: string[],

    // 申请退款退货原因
    orderBackmoneyGoodsReasons: string[]

    // 客服电话
    service_phone: string
}

export default {
    namespace: "config",
    state: {},
    reducers: {
        setState(state: ConfigState, { payload }) {
            return {...state, ...payload}
        },

    },
    effects: {
        *requestInit({}, { put, call }) {
            try {
                let ret: Response = yield call(Api.init)
                if (!ret.issuccess) {
                    Taro.showToast({ title: ret.msg, icon: "none" })
                    return
                }
                let model: InitModel = ret.data

                yield put({type: "setState", payload: {
                    orderCloseReasons: model.orderclosereason.split("\r\n"),
                    orderBackmoneyReasons: model.orderbackmoneyreason.split("\r\n"),
                    orderBackmoneyGoodsReasons: model.orderbackmoneyandgoodsreason.split("\r\n"),
                    service_phone: model.service_phone
                }})

            } catch(error) {
                console.log(error);
            }
        }
    }
}